<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form [formGroup]="keyFilterFormGroup" (ngSubmit)="save()" style="width: 1200px;">
  <mat-toolbar color="primary">
    <h2>{{(data.isAdd ? 'filter.add-key-filter' : (data.readonly ? 'filter.key-filter' : 'filter.edit-key-filter')) | translate}}</h2>
    <span class="flex-1"></span>
    <button mat-icon-button
            (click)="cancel()"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <div mat-dialog-content>
    <fieldset [disabled]="isLoading$ | async" class="flex flex-col">
      <section class="entity-key flex flex-row gap-2">
        <section class="flex flex-full flex-row gap-2"
                 [class]="{
                    'max-w-45%': isConstantKeyType,
                    'max-w-70%': !isConstantKeyType
                 }"
                 formGroupName="key">
          <mat-form-field class="mat-block max-w-40% flex-full">
            <mat-label translate>filter.key-type.key-type</mat-label>
            <mat-select required formControlName="type">
              <mat-option *ngFor="let type of entityKeyTypes" [value]="type">
                {{entityKeyTypeTranslations.get(type) | translate}}
              </mat-option>
            </mat-select>
          </mat-form-field>
          <mat-form-field class="mat-block max-w-60% flex-full">
            <mat-label translate>filter.key-name</mat-label>
            <input matInput required formControlName="key"
                   #keyNameInput
                   (focusin)="onFocus()"
                   [matAutocomplete]="keyName"
                   [matAutocompleteDisabled]="!showAutocomplete">
            <button *ngIf="keyFilterFormGroup.get('key.key').value && showAutocomplete"
                    type="button"
                    matSuffix mat-icon-button aria-label="Clear"
                    (click)="clear()">
              <mat-icon class="material-icons">close</mat-icon>
            </button>
            <mat-autocomplete autoActiveFirstOption #keyName="matAutocomplete">
              <mat-option *ngFor="let keyName of filteredKeysName | async" [value]="keyName">
                <span [innerHTML]="keyName | highlight:searchText"></span>
              </mat-option>
            </mat-autocomplete>
            <mat-error *ngIf="keyFilterFormGroup.get('key.key').hasError('required')">
              {{ 'filter.key-name-required' | translate }}
            </mat-error>
          </mat-form-field>
        </section>
        <mat-form-field class="mat-block tb-value-type flex-full"
                        [class]="{
                          'max-w-20%': isConstantKeyType,
                          'max-w-30%': !isConstantKeyType
                        }">
          <mat-label translate>filter.value-type.value-type</mat-label>
          <mat-select formControlName="valueType">
            <mat-select-trigger>
              <mat-icon class="tb-mat-18" svgIcon="{{ entityKeyValueTypes.get(keyFilterFormGroup.get('valueType').value)?.icon }}"></mat-icon>
              <span>{{ entityKeyValueTypes.get(keyFilterFormGroup.get('valueType').value)?.name | translate }}</span>
            </mat-select-trigger>
            <mat-option *ngFor="let valueType of entityKeyValueTypesKeys" [value]="valueType">
              <mat-icon class="tb-mat-18" svgIcon="{{ entityKeyValueTypes.get(entityKeyValueTypeEnum[valueType]).icon }}"></mat-icon>
              <span>{{ entityKeyValueTypes.get(entityKeyValueTypeEnum[valueType]).name | translate }}</span>
            </mat-option>
          </mat-select>
          <mat-error *ngIf="keyFilterFormGroup.get('valueType').hasError('required')">
            {{ 'filter.value-type-required' | translate }}
          </mat-error>
        </mat-form-field>
        <div class="flex-1" *ngIf="isConstantKeyType">
          <div class="flex flex-col" [ngSwitch]="keyFilterFormGroup.get('valueType').value">
            <ng-template [ngSwitchCase]="entityKeyValueTypeEnum.STRING">
              <mat-form-field class="mat-block">
                <mat-label translate>filter.value</mat-label>
                <input matInput required formControlName="value" placeholder="{{'filter.value' | translate}}">
              </mat-form-field>
            </ng-template>
            <ng-template [ngSwitchCase]="entityKeyValueTypeEnum.NUMERIC">
              <mat-form-field class="mat-block">
                <mat-label translate>filter.value</mat-label>
                <input required type="number" matInput formControlName="value">
              </mat-form-field>
            </ng-template>
            <ng-template [ngSwitchCase]="entityKeyValueTypeEnum.DATE_TIME">
              <tb-datetime formControlName="value"
                           dateText="{{ 'filter.date' | translate }}"
                           required></tb-datetime>
            </ng-template>
            <ng-template [ngSwitchCase]="entityKeyValueTypeEnum.BOOLEAN">
              <mat-checkbox formControlName="value" style="margin-top: 20px">
                {{ (keyFilterFormGroup.get('value').value ? 'value.true' : 'value.false') | translate }}
              </mat-checkbox>
            </ng-template>
          </div>
        </div>
      </section>
      <tb-filter-predicate-list *ngIf="keyFilterFormGroup.get('valueType').value"
                                [allowUserDynamicSource]="data.allowUserDynamicSource"
                                [onlyUserDynamicSource]="isConstantKeyType"
                                [displayUserParameters]="data.displayUserParameters"
                                [valueType]="keyFilterFormGroup.get('valueType').value"
                                [key]="keyFilterFormGroup.get('key.key').value"
                                formControlName="predicates">
      </tb-filter-predicate-list>
    </fieldset>
  </div>
  <div mat-dialog-actions class="flex items-center justify-end">
    <button mat-button color="primary"
            type="button"
            [disabled]="(isLoading$ | async)"
            (click)="cancel()"
            cdkFocusInitial>
      {{ (data.readonly ? 'action.close' : 'action.cancel') | translate }}
    </button>
    <button mat-raised-button color="primary"
            type="submit"
            *ngIf="!data.readonly"
            [disabled]="(isLoading$ | async) || keyFilterFormGroup.invalid || !keyFilterFormGroup.dirty">
      {{ (data.isAdd ? 'action.add' : 'action.update') | translate }}
    </button>
  </div>
</form>
